<?PHP
	require 'includes/master.inc.php';
	$auth->admin();

	if(isset($_POST['btnsave']))
	{
		$username = mysql_real_escape_string($_POST['username'], $db->db);
		if($db->getValue("SELECT COUNT(*) FROM users WHERE username = '$username' AND user_id <> '{$auth->user_id}'") > 0)
			$Error->add("username", "That username already exists.");
		$Error->passwords("password1", $_POST['password1'], $_POST['password2']);

		if($Error->ok())
		{
			$auth->user->username = $_POST['username'];

			if(!empty($_POST['password1']))
			{
				$auth->user->password = $auth->makePassword($_POST['password1']);
				$auth->user->update();
				$auth->login($auth->user->username, $_POST['password1']);
			}
			else
			{
				$auth->user->update();
			}

			$msg = "<p class='alert'>Your settings have been saved!</p>";
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Settings | Appcaster</title>
	<?PHP include 'inc/meta.inc.php'; ?>
</head>

<body id="home">
	<?PHP include 'inc/head.inc.php'; ?>
	<div id="nav">
		<ul>
			<li><a href='<?PHP WEBROOT();?>index.php'>Applications</a></li>
			<li><a href='<?PHP WEBROOT();?>orders.php'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>feedback.php'>Feedback (<?PHP echo Feedback::newCountTotal();?>)</a></li>
			<li><a href='<?PHP WEBROOT();?>stats.php'>Stats</a></li>
			<li><a href='<?PHP WEBROOT();?>options.php'>Options</a></li>
		</ul>
	</div>

	<div id="menutabs">
		<ul id="tabnav">
			<li class="selected"><a href='<?PHP WEBROOT();?>settings.php'>Your Settings</a></li>
		</ul>
	</div>

	<div id="contain">
		<form action="" method="post">
			<?PHP if(isset($msg)) echo $msg; ?>
			<fieldset>
				<legend>Login Info</legend>
				<p class="align"><label for="username">Username:</label> <input type="text" class="text" name="username" value="<?PHP echo $auth->username;?>" id="username"/></p>
				<p class="align"><label for="password1">Change Password:</label> <input type="password" class="text" name="password1" value="" id="password1"/></p>
				<p class="align"><label for="password1">Confirm Password:</label> <input type="password" class="text" name="password2" value="" id="password2"/></p>
			</fieldset>
			<p><input type="submit" name="btnsave" value="Save Settings" id="btnsave"/></p>
		</form>
	</div>
</body>
</html>